package src

import (
	"fmt"
	"net/http"
	"appengine"
	"encoding/json"
	"utils"
	"time"
)

func init () {
	http.HandleFunc("/craw/readme", crawReadmeHandler);
}

func crawReadmeHandler (w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	rm := r.FormValue("rm") 
	c.Infof("Requested Param: %v", rm)
	fmt.Fprintln(w, rm);
	
	var readme utils.Readme
	b := []byte(rm)
	
	err_json := json.Unmarshal(b, &readme)
	if err_json != nil {
		panic(err_json.Error())
	}
	
	/** INSERT, GET AND DELETE DATA **/
	//Get content readme and insert
	utils.GetReadmeContent(r, &readme)
	readme.Created = time.Now()
	if (readme.Content != "") {
		key := utils.InsertReadme(readme, r)
		
		c.Infof("Insert success readme Id: %v", key)		
	}
	/*id := int64(365)
	readme = utils.GetReadme(id, r)
	fmt.Fprintln(w, readme.Id)
	fmt.Fprintln(w, readme.Content)
	
	utils.DeleteReadme(id, r)*/
	
}